/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package xq.gwt.ui.widgets.property;

import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;

import xq.gwt.mvc.controller.TextPropertyController;
import xq.gwt.mvc.model.PropertyModel;
import xq.gwt.mvc.view.TextPropertyView;

/**
 *
 * @author admin
 */
public class TextItemPropertyPresenter extends FormItemPropertyPresenter implements TextPropertyView{
    protected TextItem textItem;
	protected TextPropertyController controller;	

    public TextItemPropertyPresenter(TextItem textItem){        
    	this.textItem = textItem;
    	formItem = textItem;
        init();
        
    }
    
    public TextItemPropertyPresenter(){  	
    	
    	textItem = new TextItem();    	
    	formItem = textItem;
    	init();
    }

	protected void init() {		
		formItem.addChangedHandler(new ChangedHandler() {
			@Override
			public void onChanged(ChangedEvent event) {
				notifyViewChangedListeners();

			}
		});		
		controller = new TextPropertyController(this);
		super.controller = controller;
	}
    
    public void setText(String text) {
        textItem.setValue(text);        
    }

    public String getText() {
    	if(textItem.getValue() == null)
    		return null;
        return String.valueOf(textItem.getValue());
    }
    
    public void setModel(PropertyModel model){    	
    	controller.setPropertyModel(model);
    }
        
    public TextItem getTextItem() {
		return textItem;
	}

    @Override
    public void setEnabled(boolean enabled) {
    	formItem.setAttribute("readOnly", !enabled);    	
    }
}
